package com.yun.zf.admin.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.BaseFont;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;

public class Html {

    public static void createPdfByUrl(OutputStream out,String htmlFilePath) throws IOException, DocumentException{
        ITextRenderer renderer = new ITextRenderer();
        String url = new File(htmlFilePath).toURI().toURL().toString();
        renderer.setDocument(url);
        // 解决中文支持问题
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont("c:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //解决图片的相对路径问题
        //renderer.getSharedContext().setBaseURL("http://localhost:8080");//file:/e:/
        renderer.layout();
        renderer.createPDF(out);
    }

    public static void main(String[] args) throws DocumentException, IOException {

        try {
        String path =System.getProperty("user.dir")+"/src/";
        StringBuffer buf = new StringBuffer();
        buf.append("<html>");
        // put in some style
        buf.append("<head><style language='text/css'>");
        buf.append("h3 { border: 1px solid #aaaaff; background: #ccccff; ");
        buf.append("padding: 1em; text-transform: capitalize; font-family: SimSun; font-weight: normal;}");
        buf.append("p { margin: 1em 1em 4em 3em; } p:first-letter { color: red; font-size: 150%; }");
        buf.append("h2 { background: #5555ff; color: white; border: 10px solid black; padding: 3em; font-size: 200%; }");
        buf.append("</style></head>");
        // generate the body
        buf.append("<body>");
        buf.append("<p><img src='"+path+"samples/100bottles.jpg'/></p>");
        for(int i=99; i>0; i--) {
            buf.append("<h3>"+i+" bottles中文字体显示测试  of beer on the wall,"
                    + i + " bottles of beer111111111122222!</h3>");
            buf.append("<p>Take one down and pass it around, "
                    + (i-1) + " bottles of beer on the wall</p>/n");
        }
        buf.append("<h2>No more bottles of beer on the wall, no more bottles of beer. ");
        buf.append("Go to 中文字体显示测试 中文字体显示测试 中文字体显示测试 the store and buy some more, 99 bottles of beer on the wall.</h2>");
        buf.append("</body>");
        buf.append("</html>");
        ITextRenderer renderer = new ITextRenderer();
        ITextFontResolver fontResolver = renderer.getFontResolver();
      fontResolver.addFont("/Users/wilson/Library/Fonts/SIMSUN.TTC", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        renderer.setDocumentFromString(buf.toString());
        String outputFile = "/Users/wilson/logs/zf_2.pdf";
        OutputStream os = null;
            os = new FileOutputStream(outputFile);
            renderer.layout();
            renderer.createPDF(os);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }


}
